package com.simple.business.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.zuihou.base.R;
import com.github.zuihou.base.service.SuperServiceImpl;
import com.simple.business.domain.dto.user.BabySaveDTO;
import com.simple.business.domain.entity.user.Baby;
import com.simple.business.domain.entity.user.UserBaby;
import com.simple.business.domain.vo.BabyVO;
import com.simple.business.mapper.user.BabyMapper;
import com.simple.business.service.user.BabyService;
import com.simple.business.service.user.UserBabyService;
import com.simple.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * @desc
 * @Author Simple
 * @Date 2025/3/10 22:17
 **/
@Slf4j
@Service
// @DS("toy")
public class BabyServiceImpl extends SuperServiceImpl<BabyMapper, Baby> implements BabyService {

    @Resource
    private UserBabyService userBabyService;

    /**
     * 查询孩子
     * @return
     */
    @Override
    public R<List<BabyVO>> queryBabies() {
        List<BabyVO> vos = this.getBaseMapper().queryBabies(SecurityUtils.getUserId());
        return R.success(vos);
    }

    /**
     * 添加孩子
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Boolean> saveOrUpdateBaby(BabySaveDTO dto) {

        Long id = dto.getId();
        Baby baby = new Baby();
        BeanUtil.copyProperties(dto, baby);

        if (Objects.isNull(id)) {
            baby.setBirthday(LocalDateTime.parse(dto.getBirthday(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            baby.setCreateUser(SecurityUtils.getUserId());
            baby.setCreateTime(LocalDateTime.now());
            save(baby);

            // 保存用户与孩子的关系
            UserBaby userBaby = new UserBaby(SecurityUtils.getUserId(), (Long) baby.getId());
            userBaby.setCreateUser(SecurityUtils.getUserId());
            userBabyService.save(userBaby);
        } else {
            updateById(baby);
        }

        return R.success();
    }

    /**
     * 批量删除
     * @param batchDeleteIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Boolean> batchDeleteBabies(List<Long> batchDeleteIds) {
        update(Wrappers.<Baby>lambdaUpdate()
                .set(Baby::getDeleted, Boolean.TRUE)
                .in(Baby::getId, batchDeleteIds));

        for (Long batchDeleteId : batchDeleteIds) {
            userBabyService.update(Wrappers.<UserBaby>lambdaUpdate()
                    .set(UserBaby::getDeleted, Boolean.TRUE)
                    .eq(UserBaby::getBabyId, batchDeleteId)
                    .eq(UserBaby::getUserId, SecurityUtils.getUserId()));
        }

        return R.success();
    }



}
